let App = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    text1:'',
    text2: '',
    text3: '',
    text4: '',
    text5: '',
    array1: '',
    array2: '',
    array3: '',
    array4: '',
    array5: '',
    address:'',
    region: '',
    type: '',
    money: '',
    house: '',
    goods:'',
  },
  //地区
  bindPickerChange1: function (e) {
  this.setData({ text1: this.data.array1[e.detail.value].name});
    this.setData({ region: this.data.array1[e.detail.value].category_id });

    let _this = this;
    App._get('cart/region', { id: this.data.array1[e.detail.value].category_id}, function (result) {
      if (result.data.region.length == 1){
        _this.setData({
          array2: result.data.region,
          text2: '暂无选项',
        });
      }else{
        _this.setData({
          array2: result.data.region,
          text2: result.data.region[0].name,
        });
      }
   
    });
  },
  //片区
  bindPickerChange2: function (e) {
    this.setData({ text2: this.data.array2[e.detail.value].name });
    this.setData({ region: this.data.array2[e.detail.value].category_id });


    let _this = this;
    App._get('cart/house', { id: this.data.array2[e.detail.value].category_id }, function (result) {
      if (result.data.house.length==1) {
        _this.setData({
          array5: result.data.house,
          text5: '暂无选项',
        });
      } else {
        _this.setData({
          array5: result.data.house,
          text5: result.data.house[0].name,
        });
      }

    });

  },
  //房间
  bindPickerChange3: function (e) {
    this.setData({ text3: this.data.array3[e.detail.value].name });
    this.setData({ type: this.data.array3[e.detail.value].id });
  },
  //改变价格
  bindPickerChange4: function (e) {
    this.setData({ text4: this.data.array4[e.detail.value].money });
    this.setData({ money: this.data.array4[e.detail.value].id });
  },

  //改变价格
  bindPickerChange5: function (e) {
    this.setData({ text5: this.data.array5[e.detail.value].name});
    this.setData({ house: this.data.array5[e.detail.value].id });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    let _this = this;
    App._get('page/category', {}, function (result) {

      _this.setData({
        array1: result.data.list,
        array2: result.data.region,
        array3: result.data.type,
        array4: result.data.money,
        array5: result.data.house,
        text1: result.data.list[0].name,
        text2: result.data.region[0].name,
        text3: result.data.type[0].name,
        text4: result.data.money[0].money,
        text5: result.data.house[0].name,
        address: result.data.list[0].category_id,
        region: result.data.region[0].category_id,
        type: result.data.type[0].id,
        money: result.data.money[0].id,
        house: result.data.house[0].id,
        goods: result.data.goods
      });
    });


  },
  btnclick:function(){
    let _this = this;
    App._get('cart/get', { 
      address: _this.data.address,
      region: _this.data.region,
      type: _this.data.type,
      money: _this.data.money,
      house: _this.data.house,
      }, 
      function (result) {
        _this.setData({
          goods: result.data.list
        });
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 获取历史搜索
   */
  getRecentSearch: function () {
    let recentSearch = wx.getStorageSync('recentSearch');
    this.setData({ recentSearch });
  },

  /**
   * 绑定输入值
   */
  getSearchContent: function (e) {
    this.data.searchValue = e.detail.value;
  },

  /**
   * 搜索提交
   */
  search: function () {
    if (this.data.searchValue) {
      // 记录最近搜索
      let recentSearch = wx.getStorageSync('recentSearch') || [];
      recentSearch.unshift(this.data.searchValue);
      wx.setStorageSync('recentSearch', recentSearch)
      // 跳转到商品列表页
      wx.navigateTo({
        url: '../category/list?search=' + this.data.searchValue,
      })
    }
  },

  /**
   * 清空最近搜索记录
   */
  clearSearch: function () {
    wx.removeStorageSync('recentSearch');
    this.getRecentSearch();
  },

  show:function(e){
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/index/manager?id='+id,
    })
  },
  /**
   * 跳转到最近搜索
   */
  goSearch: function (e) {
    wx.navigateTo({
      url: '../category/list?search=' + e.target.dataset.text,
    })
  },
  /**
     * 用户点击右上角分享
     */
  onShareAppMessage: function () {

  },
})